/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.odg.dg.xml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import org.opu.odg.dg.xml.TextMarkupUtil.Lexem;

/**
 *
 * @author yura
 */
public class LocalXpath {

    private TextMarkupUtil.Lexem lexem;
    private TextElement elem;
    private List<XmlTag> fullPaht;

    public LocalXpath(Lexem lexem, TextElement elem) {
        this.lexem = lexem;
        this.elem = elem;

        fullPaht = new ArrayList<XmlTag>();
        XmlTag e = elem.getParent();
        while (e != null) {
            fullPaht.add(e);
            e = e.getParent();
        }

        Collections.reverse(fullPaht);
        fullPaht = Collections.unmodifiableList(fullPaht);
    }

    public TextElement getElem() {
        return elem;
    }

    public List<XmlTag> getFullPaht() {
        return fullPaht;
    }

    public Lexem getLexem() {
        return lexem;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder().append("//");

        for (XmlTag xmlTag : fullPaht) {
            sb.append(xmlTag.getName()).append('/');
        }

        sb.append(elem).append('@').append(lexem);

        return sb.toString();
    }
}
